﻿

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


namespace WebApp.admin.goods
{
    public partial class goodsCategoryNavigator : BaseAdmin
    {

        protected int menuCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["MenuCategoryMaxCount"]);
        protected int moduleCategoryMaxCount = 0;

        protected int newsCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["NewsCategoryMaxCount"]);
        protected int promotionCategoryMaxCount = Convert.ToInt32(ConfigurationManager.AppSettings["PromotionCategoryMaxCount"]);

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ArrayList list;
                DataTable navigationCategoryByNavigationType;
                ArrayList list2;
                tbNavigationCategory category;
                if (base.Request.Form["menuCategory"] != null)
                {
                    list = ArrayList.Adapter(base.Request.Form["menuCategory"].Split(new char[] { ',' }));
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(0, int.Parse(base.Administrator.CompanyId));
                    list2 = new ArrayList();
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        list2.Add(Convert.ToInt32(row["category"]));
                        if (!list.Contains(row["category"].ToString()))
                        {
                            BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                        }
                    }
                    foreach (string str in list)
                    {
                        category = new tbNavigationCategory();
                        category.category = Convert.ToInt32(str);
                        category.navigationType = 0;
                        category.CompanyId = int.Parse(base.Administrator.CompanyId);
                        if (!list2.Contains(category.category))
                        {
                            BaseFactory.theNavigationCategory.Add(category);
                        }
                    }
                }
                else
                {
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(0, int.Parse(base.Administrator.CompanyId));
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                    }
                }
                if (base.Request.Form["newsCategory"] != null)
                {
                    list = ArrayList.Adapter(base.Request.Form["newsCategory"].Split(new char[] { ',' }));
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(1, int.Parse(base.Administrator.CompanyId));
                    list2 = new ArrayList();
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        list2.Add(Convert.ToInt32(row["category"]));
                        if (!list.Contains(row["category"].ToString()))
                        {
                            BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                        }
                    }
                    foreach (string str in list)
                    {
                        category = new tbNavigationCategory();
                        category.category = Convert.ToInt32(str);
                        category.navigationType = 1;
                        category.CompanyId = int.Parse(base.Administrator.CompanyId);
                        if (!list2.Contains(category.category))
                        {
                            BaseFactory.theNavigationCategory.Add(category);
                        }
                    }
                }
                else
                {
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(1, int.Parse(base.Administrator.CompanyId));
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                    }
                }
                if (base.Request.Form["promotionCategory"] != null)
                {
                    list = ArrayList.Adapter(base.Request.Form["promotionCategory"].Split(new char[] { ',' }));
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(2, int.Parse(base.Administrator.CompanyId));
                    list2 = new ArrayList();
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        list2.Add(Convert.ToInt32(row["category"]));
                        if (!list.Contains(row["category"].ToString()))
                        {
                            BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                        }
                    }
                    foreach (string str in list)
                    {
                        category = new tbNavigationCategory();
                        category.category = Convert.ToInt32(str);
                        category.navigationType = 2;
                        category.CompanyId = int.Parse( base.Administrator.CompanyId);
                        if (!list2.Contains(category.category))
                        {
                            BaseFactory.theNavigationCategory.Add(category);
                        }
                    }
                }
                else
                {
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(2, int.Parse(base.Administrator.CompanyId));
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                    }
                }
                if (base.Request.Form["moduleCategory"] != null)
                {
                    list = ArrayList.Adapter(base.Request.Form["moduleCategory"].Split(new char[] { ',' }));
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(3, int.Parse(base.Administrator.CompanyId));
                    list2 = new ArrayList();
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        list2.Add(Convert.ToInt32(row["category"]));
                        if (!list.Contains(row["category"].ToString()))
                        {
                            BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                        }
                    }
                    foreach (string str in list)
                    {
                        category = new tbNavigationCategory();
                        category.category = Convert.ToInt32(str);
                        category.navigationType = 3;
                        category.CompanyId = int.Parse( base.Administrator.CompanyId);
                        if (!list2.Contains(category.category))
                        {
                            BaseFactory.theNavigationCategory.Add(category);
                        }
                    }
                }
                else
                {
                    navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(3, int.Parse(base.Administrator.CompanyId));
                    foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                    {
                        BaseFactory.theNavigationCategory.Delete(Convert.ToInt32(row["id"]));
                    }
                }
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"goodsCategoryNavigator.aspx\";</script>");
            }
            catch
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");location.href=\"goodsCategoryNavigator.aspx\";</script>");
            }
        }

        protected StringBuilder MenuCategoryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow("00000000-0000-0000-0000-000000000000", int.Parse(base.Administrator.CompanyId), -1);
                DataTable navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(0, int.Parse(base.Administrator.CompanyId));
                ArrayList list = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                {
                    list.Add(Convert.ToInt32(row["category"]));
                }
                foreach (DataRow row in table.Rows)
                {
                    if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"menuCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                    }
                    else
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"menuCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder ModuleCategoryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow("00000000-0000-0000-0000-000000000000", int.Parse(base.Administrator.CompanyId), -1);
                DataTable navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(3, int.Parse(base.Administrator.CompanyId));
                ArrayList list = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                {
                    list.Add(Convert.ToInt32(row["category"]));
                }
                foreach (DataRow row in table.Rows)
                {
                    if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"moduleCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                    }
                    else
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"moduleCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder NewsCategoryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow("00000000-0000-0000-0000-000000000000", int.Parse(base.Administrator.CompanyId), -1);
                DataTable navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(1, int.Parse(base.Administrator.CompanyId));
                ArrayList list = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                {
                    list.Add(Convert.ToInt32(row["category"]));
                }
                foreach (DataRow row in table.Rows)
                {
                    if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"newsCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                    }
                    else
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"newsCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.GoodsCategoryNavigator);
                XmlDocument document = new XmlDocument();
                document.Load(base.Server.MapPath("~/cn/xml/home.xml"));
                this.moduleCategoryMaxCount = Convert.ToInt32(((XmlElement)document.SelectSingleNode("home_ad/category_ad")).GetAttribute("count"));
            }
        }

        protected StringBuilder PromotionCategoryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow("00000000-0000-0000-0000-000000000000", int.Parse(base.Administrator.CompanyId), -1);
                DataTable navigationCategoryByNavigationType = BaseFactory.theNavigationCategory.GetNavigationCategoryByNavigationTypeAndCompanyId(2, int.Parse(base.Administrator.CompanyId));
                ArrayList list = new ArrayList();
                foreach (DataRow row in navigationCategoryByNavigationType.Rows)
                {
                    list.Add(Convert.ToInt32(row["category"]));
                }
                foreach (DataRow row in table.Rows)
                {
                    if (list.Contains(Convert.ToInt32(row["categoryIdentity"])))
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"promotionCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" checked=\"checked\" />", row["categoryName"], "" }));
                    }
                    else
                    {
                        builder.AppendLine(string.Concat(new object[] { "<input id=\"cb", row["categoryIdentity"], "\" name=\"promotionCategory\" value=\"", row["categoryIdentity"], "\" type=\"checkbox\" />", row["categoryName"], "" }));
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

    }
}